<?php

namespace Home\Service;

use Home\Common\FIdConst;
use Home\DAO\GoodsBomDAO;

/**
 * 订单Service
 *
 * @author 蔺博
 */
class BillService extends PSIBaseService {
	private $LOG_CATEGORY_UNIT = "基础数据-订单类型";

	/**
	 * 返回所有订单类型
	 */
	public function allTypes() {
		if ($this->isNotOnline()) {
			return $this->emptyResult();
		}
		
		$db = M();
		$sql = "select id, name 
				from t_bill_type 
				order by convert(name USING gbk) collate gbk_chinese_ci";
		
		return $db->query($sql);
	}

    /**
     * 新建或者编辑 订单类型
     */
    public function editType($params) {
        if ($this->isNotOnline()) {
            return $this->notOnlineError();
        }

        $id = $params["id"];
        $name = $params["name"];

        $db = M();
        $db->startTrans();

        $log = null;

        if ($id) {
            // 编辑
            // 检查计量单位是否存在
            $sql = "select count(*) as cnt from t_bill_type where name = '%s' and id <> '%s' ";
            $data = $db->query($sql, $name, $id);
            $cnt = $data[0]["cnt"];
            if ($cnt > 0) {
                $db->rollback();
                return $this->bad("订单类型 [$name] 已经存在");
            }

            $sql = "update t_bill_type set name = '%s' where id = '%s' ";
            $rc = $db->execute($sql, $name, $id);
            if ($rc === false) {
                $db->rollback();
                return $this->sqlError(__LINE__);
            }

            $log = "编辑订单类型: $name";
        } else {
            // 新增
            // 检查计量单位是否存在
            $sql = "select count(*) as cnt from t_bill_type where name = '%s' ";
            $data = $db->query($sql, $name);
            $cnt = $data[0]["cnt"];
            if ($cnt > 0) {
                $db->rollback();
                return $this->bad("订单类型 [$name] 已经存在");
            }

//            $idGen = new IdGenService();
//            $id = $idGen->newId();

//            $us = new UserService();
//            $dataOrg = $us->getLoginUserDataOrg();
//            $companyId = $us->getCompanyId();

            $sql = "insert into t_bill_type(id, name, description, createtime) 
					values ('%s', '%s', '%s', '%s') ";
            $rc = $db->execute($sql, $id, $name, $name, time());
            if ($rc === false) {
                $db->rollback();
                return $this->sqlError(__LINE__);
            }

            $log = "新增订单类型: $name";
        }

        // 记录业务日志
        if ($log) {
            $bs = new BizlogService();
            $bs->insertBizlog($log, $this->LOG_CATEGORY_UNIT);
        }

        $db->commit();

        return $this->ok($id);
    }

    /**
     * 删除订单类型
     */
    public function deleteType($params) {
        if ($this->isNotOnline()) {
            return $this->notOnlineError();
        }

        $id = $params["id"];

        $db = M();
        $db->startTrans();

        $sql = "select name from t_bill_type where id = '%s' ";
        $data = $db->query($sql, $id);
        if (! $data) {
            $db->rollback();
            return $this->bad("要删除的订单类型不存在");
        }
        $name = $data[0]["name"];

        // 检查否被使用
//        $sql = "select count(*) as cnt from t_goods where unit_id = '%s' ";
//        $data = $db->query($sql, $id);
//        $cnt = $data[0]["cnt"];
//        if ($cnt > 0) {
//            $db->rollback();
//            return $this->bad("商品计量单位 [$name] 已经被使用，不能删除");
//        }

        $sql = "delete from t_bill_type where id = '%s' ";
        $rc = $db->execute($sql, $id);
        if ($rc === false) {
            $db->rollback();
            return $this->sqlError(__LINE__);
        }

        $log = "删除订单类型: $name";
        $bs = new BizlogService();
        $bs->insertBizlog($log, $this->LOG_CATEGORY_UNIT);

        $db->commit();

        return $this->ok();
    }

    /**
     * 订单类型字段， 查询数据
     */
    public function queryData($queryKey) {
        if ($this->isNotOnline()) {
            return $this->emptyResult();
        }

        if ($queryKey == null) {
            $queryKey = "";
        }

        $sql = "select id, name, description, createtime  
				from t_bill_type
				where (id like '%s' or name like '%s') ";
        $queryParams = array();
        $key = "%{$queryKey}%";
        $queryParams[] = $key;
        $queryParams[] = $key;
        $queryParams[] = $key;

        $sql .= " order by id 
				limit 20";
        return M()->query($sql, $queryParams);
    }

}